﻿using Inaction.Message;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;

namespace Inaction
{
    public static class ClientUpdator
    {
        private static List<FileDescription> GetDirectoryFileDescriptions(string path = null)
        {
            if (string.IsNullOrEmpty(path)) path = Util.AppHelper.UpdateDirectory;
            List<FileDescription> list = new List<FileDescription>();
            var files = Directory.GetFiles(path, "*", SearchOption.AllDirectories);
            foreach (var file in files)
            {
                FileInfo fileInfo = new FileInfo(file);
                var fileDesecription = new FileDescription()
                {
                    FilePath = file.Replace(path, ""),
                    Name = fileInfo.Name,
                    UpdateTime = fileInfo.LastWriteTimeUtc
                };
                list.Add(fileDesecription);
            }
            return list;
        }
        private static byte[] ReadFile(FileDescription fileDesecription)
        {
            var filePath = Util.AppHelper.UpdateDirectory + fileDesecription.FilePath;
            if (File.Exists(filePath)) return File.ReadAllBytes(filePath);
            else return null;
        }
        public static void Update()
        {
            MessageHost.ShowMessage("正在检查更新文件...");
            var files = RemotingMethod.Invoke<List<FileDescription>>(new Func<string, List<FileDescription>>(GetDirectoryFileDescriptions), string.Empty);
            if (files.Count > 0)
            {
                MessageHost.ShowMessage("正在比较更新文件...");
                var localFiles = GetDirectoryFileDescriptions(Util.AppHelper.ExecuteDirectory);
                List<FileDescription> needUpdateFiles = new List<FileDescription>();

                foreach (var file in files)
                {
                    var localFile = localFiles.FirstOrDefault(p => p.FilePath == file.FilePath && p.Name.Equals(file.Name, StringComparison.OrdinalIgnoreCase));
                    if (localFile == null)
                    {
                        needUpdateFiles.Add(file);
                    }
                    else if (file.UpdateTime.ToUniversalTime() > localFile.UpdateTime.ToUniversalTime())
                    {
                        needUpdateFiles.Add(file);
                    }
                }
                if (needUpdateFiles.Count != 0)
                {
                    foreach (var file in needUpdateFiles)
                    {
                        MessageHost.ShowMessage("正在更新:" + file.Name);
                        var data = RemotingMethod.Invoke<byte[]>(new Func<FileDescription, byte[]>(ReadFile), file);
                        if (data != null && data.Length > 0)
                        {
                            File.WriteAllBytes(file.FilePath, data);
                        }
                    }
                }
            }
        }
    }
    [Serializable]
    public class FileDescription
    {
        public string Name { get; set; }
        public string FilePath { get; set; }
        public DateTime UpdateTime { get; set; }
    }
}
